import random
import requests
import ddddocr

headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "zh-CN,zh;q=0.9",
    "cache-control": "no-cache",
    "content-type": "application/x-www-form-urlencoded",
    "origin": "https://spxksq.amr.guizhou.gov.cn:9081",
    "referer": "https://spxksq.amr.guizhou.gov.cn:9081/TopFDOAS/searchDoc.action",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
}
# cookies = {
#     "JSESSIONID": "65BE9FFCE6EF072DC835F6E0D83FFA21",
#     "29425169-aff8-4e6a-9808-d56a8435899e": "WyIyNDQwMzM0NDU1Il0"
# }
url = "https://spxksq.amr.guizhou.gov.cn:9081/TopFDOAS/searchDoc.action"
img_url = f'https://spxksq.amr.guizhou.gov.cn:9081/TopFDOAS/randMouth.action?id={random.random()}'
# 需携带上次请求的cookies, 否则会出现验证码错误
ses = requests.session()
res = ses.get(img_url, headers=headers).content
with open('yzm.png', 'wb') as f:
    f.write(res)
ocr = ddddocr.DdddOcr(show_ad=False, beta=True)
f = open('yzm.png', 'rb').read()
yzm = ocr.classification(f)
print('验证码为:', yzm)

data = {
    "searchFlag": "true",
    "filterStr": "~,!,$,^,+,<,>,”,&,#,@,NULL,0x0d,0x0a",
    "nameLicNo": "多彩贵州文化艺术股份有限公司",
    "yzm": yzm
}
response = ses.post(url, headers=headers, data=data)

print(response.text)
print(response)